import com.lbi.core.events.IEvents;
import com.lbi.framework.mvc.collection.Collection;

import asunit.framework.TestCase;

import framework.collection.DummySelectable;
import com.lbi.core.helper.Delegate;
import com.lbi.framework.mvc.collection.Selectable;

/**
 * @author michaelforrest
 */
class framework.collection.CollectionTest extends TestCase {
	private var className : String = "CollectionTest";

	private var collection : Collection;

	private var first : DummySelectable;
	public function CollectionTest(testMethod : String) {
		super(testMethod);
	}
	private function setUp() :Void{
		collection = new Collection();
		var items : Array = ["one","two","three","four"];
		for (var i : Number = 0; i < items.length; i++) {
			var id:String = items[i];
			collection.push(new DummySelectable(id));			
		}
		assertUndefined("nothing should be selected initially - found " + collection.current(),
					collection.current());
		first = DummySelectable(collection.first());
		assertTrue(first instanceof DummySelectable);
		assertEquals("one",first.id);
		collection.select(first);
	}
	private function tearDown() :Void{
		delete collection;	
	}
	private function testCollectionSelections() :Void{
		
		
		assertEquals(0,collection.getCurrentIndex());
		var current : DummySelectable = DummySelectable(collection.current());
		assertTrue("current() should return a DummySelectable - we got " + current,
					current instanceof DummySelectable);
		assertEquals("expected the first item to be selected. but it wasn't",
					"one",current.id);
	}
	private function testCollectionEvents() :Void{
		
		checkEvent(collection,Collection.CHANGED, collection.next, "collection should dispatch changed when next() is called");
		checkEvent(collection,Collection.INCREASED, collection.next, "collection should dispatch changed when next() is called");
		
	}
	private function testItemEvents() :Void{
		var selected_worked : Boolean = false;
		var deselected_worked : Boolean = false;
		first.addEventListener(Selectable.SELECTED,function(){
			selected_worked = true;	
		});
		first.addEventListener(Selectable.DESELECTED,function(){
			deselected_worked = true;
		});
		collection.next();
		assertTrue(deselected_worked);
		collection.select(first);
		assertTrue(selected_worked);
	}

	private function checkEvent(object : IEvents, event : String, call : Function,message:String) : Void {
		var test : Boolean = false;
		object.addEventListener(event,function(){
			test = true;
		});
		Delegate.create(object,call)();
		assertTrue(message,test);
	}

}